namespace Panorama.Data.Migrations
{
    using System;
    using System.Data.Entity.Migrations;
    using System.Linq;
    using Panorama.Data.Identity;
    using Microsoft.AspNet.Identity.EntityFramework;
    using Microsoft.AspNet.Identity;

    public sealed class Configuration : DbMigrationsConfiguration<Panorama.Data.Identity.PanoramaDb>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
            AutomaticMigrationDataLossAllowed = true;
        }

        protected override void Seed(Panorama.Data.Identity.PanoramaDb context)
        {
            var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new PanoramaDb()));
            
          if (!context.Roles.Any(r => r.Name == "Admin" || r.Name=="User"))
        {
            var store = new RoleStore<IdentityRole>(context);
            var roleManager = new RoleManager<IdentityRole>(store);
            
            var adminRole = new IdentityRole { Name = "Admin" };
            var userRole = new IdentityRole { Name = "User" };
            
            roleManager.Create(adminRole);
            roleManager.Create(userRole);
        }

                     
            if (manager.FindByName("master") == null)
            {
                var user = new ApplicationUser() { UserName = "master", LockoutEnabled = false };
                manager.Create(user, "123456");
                manager.AddToRole(user.Id, "Admin");
            }
        }
    }
}